import allure
from selenium.webdriver.common.by import By

from page.home_page import HomePage
from utils.logger import logger


class GoodsListPage(HomePage):
    _PRODUCT = (By.CSS_SELECTOR, ".el-input__inner")
    _WAREHOUSE = (By.XPATH, '//*[@placeholder="请选择"]')
    _SUBMIT = (By.XPATH, '//*[text()="提交"]')
    _TEXT = (By.XPATH, '//*[text()="创建成功"]')
    _ADD = (By.XPATH, "//*[text()='添加']")

    @allure.step("添加商品信息")
    def create_goods(self, warehouse, goods_name, form_name, price, inventory):
        """
        :param warehouse: 仓库名称
        :param goods_name: 商品名称
        :param form_name: 规格名称
        :param price: 价格
        :param inventory: 库存
        :return:
        """
        self.set_time(2)
        self.find_and_click(self._ADD)
        self.set_time(1)

        logger.info("选择仓库")
        with allure.step("选择仓库"):
            self.set_time(2)
            self.find(self._WAREHOUSE).send_keys(warehouse)
            self.find_and_click(By.XPATH, f"//*[text()='{warehouse}']")

        with allure.step("输入商品信息"):
            self.finds(self._PRODUCT)[1].send_keys(goods_name)
            self.finds(self._PRODUCT)[6].send_keys(form_name)
            self.finds(self._PRODUCT)[8].clear()
            self.finds(self._PRODUCT)[8].send_keys(price)
            self.finds(self._PRODUCT)[9].clear()
            self.finds(self._PRODUCT)[9].send_keys(inventory)
        logger.info("点击提交按钮")
        self.find_and_click(self._SUBMIT)
        self.set_time(1)
        text = self.get_text(self._TEXT)
        return text

    _SEARCH = (By.XPATH, '//*[text()="查询"]')
    _GOODS_NAME = (By.XPATH, '//*[@placeholder="请输入商品名称"]')
    _FROM_NAME = (By.XPATH, "//*[@placeholder='请输入规格名称']")
    _NUM = (By.CSS_SELECTOR, '.el-pagination__total')

    @allure.step("输入筛选条件,查询")
    def search(self, goods_name, from_name):
        """

        :param goods_name: 商品名称
        :param from_name: 规格名称
        :return:
        """
        self.set_time(1)
        logger.info("输入筛选条件，查询")
        with allure.step("输入筛选条件，查询"):
            self.find_and_send(goods_name, self._GOODS_NAME)
            self.find_and_send(from_name, self._FROM_NAME)
            self.find_and_click(self._SEARCH)
            self.set_time(1)
        return self.finds(self._NUM)[1].text

    @allure.step("输入筛选条件,查询")
    def searchs(self, goods_name, from_name):
        """

        :param goods_name: 商品名称
        :param from_name: 规格名称
        :return:
        """
        self.set_time(1)
        logger.info("输入筛选条件，查询")
        with allure.step("输入筛选条件，查询"):
            self.find_and_send(goods_name, self._GOODS_NAME)
            self.find_and_send(from_name, self._FROM_NAME)
            self.find_and_click(self._SEARCH)
            self.set_time(1)
        return self

    _OPEN = (By.XPATH, '//*[text()="允许拆合单"]')
    _CLOSE = (By.XPATH, '//*[text()="不允许拆合单"]')
    _SAVE = (By.XPATH, "//*[text()='保存']")
    _TEXTS = (By.XPATH, "//*[text()='操作成功']")
    _拆合单 = (By.XPATH, "//*[text()='拆合单规则']")

    @allure.step("开启拆合单")
    def open_split_merge(self):
        self.set_time(1)
        # self.find_and_click(self._拆合单)
        element = self.find(self._拆合单)
        # arguments[0]代表所传值element的第一个参数
        # click()代表js中的点击动作
        self._driver.execute_script("arguments[0].click();", element)
        logger.info("开启拆合单")
        with allure.step("开启拆合单"):
            self.find_and_click(self._OPEN)
            self.find_and_click(self._SAVE)
            self.set_time(1)
        return self.get_text(self._TEXTS)

    @allure.step("关闭拆合单")
    def close_split_merge(self):
        self.set_time(1)
        element = self.find(self._拆合单)
        # arguments[0]代表所传值element的第一个参数
        # click()代表js中的点击动作
        self._driver.execute_script("arguments[0].click();", element)
        logger.info("关闭拆合单")
        with allure.step("关闭折合单"):
            self.find_and_click(self._OPEN)
            self.find_and_click(self._SAVE)
            self.set_time(1)
        return self.get_text(self._TEXTS)

    _REMOVE = (By.XPATH, '//*[text()="下架"]')
    _COMMIT = (By.XPATH,'//*[text()="提交"]')

    @allure.step("下架商品")
    def remove_goods(self):

        with allure.step("下架商品"):
            element = self.find(self._REMOVE)
            # arguments[0]代表所传值element的第一个参数
            # click()代表js中的点击动作
            self._driver.execute_script("arguments[0].click();", element)
            # self.find_and_click(self._REMOVE)
            self.find_and_click(self._COMMIT)
            self.set_time(1)
            result = self.get_text(self._TEXTS)
            # self._driver.back()
        return result

    _ALREADY_REMOVE = (By.XPATH,"//*[text()='已下架商品']")
    @allure.step("上架商品")
    def already_remove(self):
        self.set_time(1)
        with allure.step("进入已下架商品"):
            self.find_and_click(self._ALREADY_REMOVE)
            self.set_time(1.5)
            return self

    _LISTING = (By.XPATH, '//*[text()="上架"]')

    @allure.step("上架商品")
    def listing_goods(self,goods_name):

        with allure.step("输入筛选条件，查询"):
            self.finds(self._GOODS_NAME)[2].clear()
            self.finds(self._GOODS_NAME)[2].send_keys(goods_name)
            self.finds(self._SEARCH)[2].click()
            self.set_time(1)
        with allure.step("上架商品"):
            element = self.find(self._LISTING)
            # arguments[0]代表所传值element的第一个参数
            # click()代表js中的点击动作
            self._driver.execute_script("arguments[0].click();", element)
            self.set_time(1)
            element = self.find(self._COMMIT)
            self._driver.execute_script("arguments[0].click();", element)
            self.set_time(1)
            result = self.get_text(self._TEXTS)
            self._driver.back()
        return result

    _ALL = (By.XPATH,'//*[text()="全选"]')
    _DEL = (By.XPATH,'//*[text()="删除商品"]')
    _CHOOSE = (By.XPATH,'//*[text()="按选择"]')
    _OK = (By.XPATH,"//*[contains(text(),'确定')]")
    @allure.step("删除商品")
    def del_goods(self):
        self.set_time(1)
        self.set_time(1)
        self.finds(self._ALL)[1].click()
        self.set_time(1)
        self.find_and_click(self._DEL)
        self.set_time(5)
        self.find_and_click(self._CHOOSE)
        self.set_time(1)
        self.find_and_click(self._OK)
        return True





